package com.iotimc.attendancesystem.manage.fragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iotimc.attendancesystem.R;
import com.iotimc.attendancesystem.entity.DoorLogEntity;
import com.iotimc.attendancesystem.entity.PersonEntity;
import com.iotimc.attendancesystem.util.PictureManager;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by cwj on 2017/11/6.
 *
 * @author cwj
 */

public class DoorManageAdapter extends RecyclerView.Adapter<DoorManageAdapter.ViewHolder> {
    private List<DoorLogEntity> info;
    private Context context;
    private LayoutInflater inflater;


    public DoorManageAdapter(List<DoorLogEntity> info, Context context) {
        this.info = info;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void addData(List<DoorLogEntity> info) {
        this.info = info;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_manage_recy_door, parent, false));
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        DoorLogEntity doorLogEntity = info.get(position);
        PersonEntity personEntity = info.get(position).getPersonEntity();
        //PictureManager.getInstance().loadCirclePic(context, personEntity.getImage(), holder.mIvManageDoorHead, false);
        byte[] image = personEntity.getImage();
        if (image == null) {
            PictureManager.getInstance().loadCirclePic(context, R.drawable.ic_user_img, holder.mIvManageDoorHead, true);

        } else {
            PictureManager.getInstance().loadCirclePic(context, image, holder.mIvManageDoorHead, true);
        }
        String code = personEntity.getCode();
        if (!TextUtils.isEmpty(code)) {
            holder.mTvManageDoorCode.setText(code);
        }
        String name = personEntity.getName();
        if (!TextUtils.isEmpty(name)) {
            holder.mTvManageDoorName.setText(name);
        }
        String card = personEntity.getCard();
        if (!TextUtils.isEmpty(card)) {
            holder.mTvManageDoorCard.setText(card);
        }

        Date time = doorLogEntity.getCreateTime();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = sdf.format(time);

        holder.mTvManageDoorOpen.setText(str);
        holder.mTvManageDoorSex.setText(info.get(position).getPersonEntity().getSex());
        if (position % 2 == 0) {
            holder.mLlManageDoor.setBackground(context.getResources().getDrawable(R.drawable.bg_manage_list));
        } else {
            holder.mLlManageDoor.setBackground(context.getResources().getDrawable(R.drawable.bg_manage_list2));
        }
    }


    @Override
    public int getItemCount() {
        return info.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView mIvManageDoorHead;
        TextView mTvManageDoorCode;
        TextView mTvManageDoorName;
        TextView mTvManageDoorSex;
        TextView mTvManageDoorOpen;
        TextView mTvManageDoorCard;
        LinearLayout mLlManageDoor;

        ViewHolder(View itemView) {
            super(itemView);
            mIvManageDoorHead = itemView.findViewById(R.id.iv_manage_door_head);
            mTvManageDoorCard = itemView.findViewById(R.id.tv_manage_door_card);
            mTvManageDoorCode = itemView.findViewById(R.id.tv_manage_door_code);
            mTvManageDoorName = itemView.findViewById(R.id.tv_manage_door_name);
            mTvManageDoorSex = itemView.findViewById(R.id.tv_manage_door_sex);
            mTvManageDoorOpen = itemView.findViewById(R.id.tv_manage_door_open);
            mLlManageDoor = itemView.findViewById(R.id.ll_manage_door);
        }
    }
}
